package com.qianduo.front.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qianduo.front.model.Daikuan;
import com.qianduo.front.service.ReadDaikuanService;

import common.util.StringUtil;

public class DaikuanServlet extends HttpServlet{
	private static final long serialVersionUID = -5247198548657877355L;
	public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{
		doPost(req,res);
	}
	public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{
		String action	= StringUtil.isEmptyRetString(req.getParameter("action"));
		String page		= null;
		if("search".equalsIgnoreCase(action)){
			List<String> errors = new ArrayList<String>();
			String cityID		= req.getParameter("cityID");
			cityID				= "197";
			//String daikuanType	= req.getParameter("daikuanType");
			String amount		= req.getParameter("amount");
			String month		= req.getParameter("month");
			/*
			if(StringUtil.isEmpty(daikuanType)){
				errors.add("贷款用途不能为空");
			}
			*/
			if(StringUtil.isEmpty(amount)){
				errors.add("贷款金额不能为空");
			}
			if(StringUtil.isEmpty(month)){
				errors.add("贷款期限不能为空");
			}
			if(errors.size() == 0){
				ReadDaikuanService service	= new ReadDaikuanService();
				List<Daikuan> list			= service.getDaikuanList(null,amount, null, month, cityID, null, null,null,null);
				
				req.setAttribute("list", list);
				page	= "/pages/jsp/daikuanList.jsp";
			}else{
				req.setAttribute("errors", errors);
				
				page	= "/pages/jsp/index.jsp";
			}
		}else if("detail".equalsIgnoreCase(action)){			
			List<String> errors = new ArrayList<String>();
			String amount	= req.getParameter("amount");
			String month	= req.getParameter("month");
			String id		= req.getParameter("id");
			if(StringUtil.isEmpty(id)){
				errors.add("id不能为空");
			}
			if(StringUtil.isEmpty(amount)){
				errors.add("贷款金额不能为空");
			}
			if(StringUtil.isEmpty(month)){
				errors.add("贷款期限不能为空");
			}

			if(errors.size() == 0){
				ReadDaikuanService service	= new ReadDaikuanService();
				List<Daikuan> list			= service.getDaikuanList(id,null, null, null, null, null, null,null,null);
				
				if(null != list && list.size() > 0){
					Daikuan dk	= list.get(0);

					req.setAttribute("daikuan", dk);
					page	= "/pages/jsp/daikuanDetail.jsp";
				}
			}		
		}
		
		if(!StringUtil.isEmpty(page)){
			RequestDispatcher dis	= req.getRequestDispatcher(page);
			dis.forward(req, res);
		}else{			
			
		}
	}
}
